<?php

namespace app\cli\controller;

use app\common\controller\Clibase;

class Mask extends Clibase
{
    protected $log_dir = 'cli/mask';

    public function initialize()
    {
        $php_sapi_name = php_sapi_name();
        $cli_eheck     = env('CLI_CHECK');
        $cli_eheck     = isset($cli_eheck) ? $cli_eheck : true;
        if ($php_sapi_name != 'cli' && $cli_eheck === true) {
            $this->error('非法访问', '/');
        }

        date_default_timezone_set('PRC');
    }

    // 测试
    public function buyMask()
    {
        $i         = 1;
        $success   = [];
        $user_info = $this->getMaskUser();
        while ($i < 2) {
            $now_time  = time();
            $stat_time = strtotime(date('Y-m-d 12:00:00'));
            $end_time  = strtotime(date('Y-m-d 12:02:00'));
            if ($now_time >= $stat_time && $now_time <= $end_time) {
                $url = 'http://tlkzyy.bestsoft.vip:8084/Handler/OrderHandler.ashx';
                foreach ($user_info as $user) {
                    if (!isset($success[$user['mobile']])) {
                        $user['op']     = 'AddOrder';
                        $user['quyu']   = '旧县街道';
                        $user['fdname'] = '旧县连锁店【旧县街道青年路1号 64223709】';

                        $microtime1 = microtime(true);
                        $res        = \util\Curl::curlPostRequest($url, $user);
                        $microtime2 = microtime(true);
                        $log_str    = '___buyMask___ date:' . date('Y-m-d H:i:s') . "\r\n";
                        $log_str    = $log_str . '请求数据：' . json_encode($user, JSON_UNESCAPED_UNICODE) . "\r\n";
                        $log_str    = $log_str . '返回结果: ' . json_encode($res, JSON_UNESCAPED_UNICODE) . "\r\n";
                        $log_str    = $log_str . '开始毫秒：' . $microtime1 . '；结束毫秒：' . $microtime2 . "\r\n";
                        printLog($log_str, $this->log_dir);

                        if (strtolower($res) == 'ok') {
                            $success[$user['mobile']] = 1;
                        }
                    }
                }

                $now_time = time();
            } elseif ($now_time < $stat_time) {
                $i = 1;
            } else {
                $i++;
                printLog('无数据' . "\r\n", $this->log_dir);
            }
        }
    }

    private function getMaskUser()
    {
        $user_info = [
            '20200309' => [
                ['name' => '朱勤康', 'mobile' => '18705202770'],
                ['name' => '宋莲丽', 'mobile' => '15204515892'],
                ['name' => '徐海泉', 'mobile' => '13957598855'],
                ['name' => '黄帆', 'mobile' => '15970691315'],
                ['name' => '李海绵', 'mobile' => '13750093062'],
                ['name' => '李恬', 'mobile' => '15850688911'],
                ['name' => '蒋淑丹', 'mobile' => '13858123459'],
                ['name' => '王颖洁', 'mobile' => '15257310783'],
                ['name' => '蒋卫文', 'mobile' => '13601888342'],
                ['name' => '史今', 'mobile' => '17611577339'],
                ['name' => '胡诗佳', 'mobile' => '15068870929'],
            ],
            '20200310' => [
                ['name' => '彭女士', 'mobile' => '13721217817'],
                ['name' => '陈生', 'mobile' => '15864009988'],
                ['name' => '李颖', 'mobile' => '13605151108'],
                ['name' => '阿聪', 'mobile' => '18858777718'],
                ['name' => '薛嬷嬷', 'mobile' => '13588841738'],
                ['name' => '刘畅', 'mobile' => '13770549017'],
                ['name' => '董海彬', 'mobile' => '13082811566'],
                ['name' => '王虹', 'mobile' => '13065726833'],
                ['name' => '焱已恬', 'mobile' => '17611309622'],
                ['name' => '姚舒琪', 'mobile' => '18768345035'],
                ['name' => '陈虹', 'mobile' => '18930916659'],
            ],
            '20200311' => [
                ['name' => '尚泽宇', 'mobile' => '18325998188'],
                ['name' => '蒋卫文', 'mobile' => '13601888342'],
                ['name' => '李小姐', 'mobile' => '13816563981'],
                ['name' => '姚诚', 'mobile' => '18652039039'],
                ['name' => '木栗', 'mobile' => '18639526122'],
                ['name' => '柯园园', 'mobile' => '15901908948'],
                ['name' => '李静雅', 'mobile' => '13365510619'],
                ['name' => '钱朝', 'mobile' => '13671709262'],
                ['name' => '蒋军芳', 'mobile' => '18019343722'],
                ['name' => '齐其', 'mobile' => '15905860023'],
                ['name' => '海绵', 'mobile' => '13486443655'],
            ],
        ];

        $date = date('Ymd');
        return $user_info[$date];
    }

}
